<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * Codeigniter: mpdf libraries
 * Tuesday, July, 19 2011
 * 
 * @author bang.webdeveloper@gmail.com
 */
require_once dirname(__FILE__) . '/mpdf/mpdf.php';

class Pdf extends mPDF
{
	var $ci;
    function __construct()
    {
        parent::__construct();
        $this->ci = $this->ci =& get_instance();
    }
    
    public function pdf_table_company($array)
    {
				
	$html = '<table border="1"><thead>
	<tr style="background-color:#22313B; color:#ffffff">
		<th style="color:#ffffff">#</th>
		<th style="color:#ffffff">Name</th>
		<th style="color:#ffffff">Country/City</th>
		<th style="color:#ffffff">Email</th>
		<th style="color:#ffffff">Website</th>
		<th style="color:#ffffff">Phone(s)</th>
		<th style="color:#ffffff">Fax(s)</th>
		<th style="color:#ffffff">Address(s)</th>
		<th style="color:#ffffff">Notes</th>
	</tr>
	</thead><tbody>';
	$i = 0;
	if(isset($array) && is_array($array))
	foreach($array as $row){
		if($i % 2)
			$style_odd = "style='background-color:#E6E6E6;'";
		else
			$style_odd = "style='background-color:#ffffff;'";
			//$is_arabic = preg_match('/\p{Arabic}/', $text);
		$html.="<tr {$style_odd}>";
		$html.="	<td>{$row->id}</td>";
		$html.="	<td>{$row->name}</td>";
		$html.="	<td><img src='{$this->ci->config->item('images')}/flags/{$row->country}.gif' /> / {$row->city}</td>";
		$html.="	<td>{$row->email}</td>";
		$html.="	<td>{$row->website}</td>";
		$html.="	<td>{$row->phone1} / {$row->phone2}/ {$row->phone3}</td>";
		$html.="	<td>{$row->fax1} / {$row->fax2}</td>";
		$html.="	<td>{$row->address1} {$row->address2}</td>";
		$html.="	<td>{$row->notes}</td>";
		$html.="</tr>";
		$i++;
	}
	
	ini_set("memory_limit","750M");
	
	$html.='</tbody></table>';
	
	
	$mpdf = new Pdf('c','A4','','',32,25,27,25,16,13);

	$mpdf->SetHeader('{DATE j-m-Y}|{PAGENO}/{nb}|Grape System');
	$mpdf->SetFooter('{PAGENO}');	/* defines footer for Odd and Even Pages - placed at Outer margin */
	$mpdf->SetDisplayMode('fullpage');

	$mpdf->SetWatermarkText('Grape System');
	$mpdf->watermark_font = 'DejaVuSansCondensed';
	$mpdf->showWatermarkText = true;
	$mpdf->useAdobeCJK = true;

	$mpdf->SetAutoFont(AUTOFONT_ALL);
	$mpdf->list_indent_first_level = 0;	// 1 or 0 - whether to indent the first level of a list
	
	// LOAD a stylesheet
	$stylesheet = file_get_contents($this->ci->config->item('css').'/mpdfstyletables.css');
	$mpdf->WriteHTML($stylesheet,1);	// The parameter 1 tells that this is css/style only and no body/html/text

	$mpdf->WriteHTML($html,2);

	
	$file_name = 'companies_'.$this->ci->session->userdata['user_id'].'_'.$this->ci->session->userdata['username'].'_'.mktime().'.pdf';
	$pdf = $mpdf->Output('./temp/'.$file_name, 'F'); 
	$data['file'] = $file_name;
	$data['result'] = $pdf;
	
	return $data;
	}
    
    
    public function pdf_table_contact($array)
    {
				
	$html = '<table border="1"><thead>
	<tr style="background-color:#22313B; color:#ffffff">
		<th style="color:#ffffff">#</th>
		<th style="color:#ffffff">Title</th>
		<th style="color:#ffffff">Name</th>
		<th style="color:#ffffff">Job Des.</th>
		<th style="color:#ffffff">Company</th>
		<th style="color:#ffffff">Gender</th>
		<th style="color:#ffffff">Country/City</th>
		<th style="color:#ffffff">Email</th>
		<th style="color:#ffffff">Phone(s)</th>
		<th style="color:#ffffff">Address(s)</th>
		<th style="color:#ffffff">Notes</th>
	</tr>
	</thead><tbody>';
	$i = 1;
	if(isset($array) && is_array($array))
	foreach($array as $row){
		if($i % 2)
			$style_odd = "style='background-color:#E6E6E6;'";
		else
			$style_odd = "style='background-color:#ffffff;'";
			//$is_arabic = preg_match('/\p{Arabic}/', $text);
		$html.="<tr {$style_odd}>";
		$html.="	<td>{$i}/{$row->id}</td>";
		$html.="	<td>{$row->title_ar}<br>{$row->title_en}</td>";
		$html.="	<td>{$row->name_ar}<br>{$row->name_en}</td>";
		$html.="	<td>{$row->job_des}</td>";
		$html.="	<td>{$row->com}</td>";
		$html.="	<td>{$row->gender}</td>";
		$html.="	<td><img src='{$this->ci->config->item('images')}/flags/{$row->country}.gif' /> / {$row->city}</td>";
		$html.="	<td>{$row->email}</td>";
		$html.="	<td>{$row->mobile1} / {$row->mobile2}/ {$row->home_phone}</td>";
		$html.="	<td>{$row->address}</td>";
		$html.="	<td>{$row->notes} {$row->extra} {$row->extra_2} {$row->extra_3}</td>";
		$html.="</tr>";
		$i++;
	}
	
	ini_set("memory_limit","750M");
	
	$html.='</tbody></table>';
	
	
	$mpdf = new Pdf('c','A4','','',32,25,27,25,16,13);

	$mpdf->SetHeader('{DATE j-m-Y}|{PAGENO}/{nb}|Grape System');
	$mpdf->SetFooter('{PAGENO}');	/* defines footer for Odd and Even Pages - placed at Outer margin */
	$mpdf->SetDisplayMode('fullpage');

	$mpdf->SetWatermarkText('Grape System');
	$mpdf->watermark_font = 'DejaVuSansCondensed';
	$mpdf->showWatermarkText = true;
	$mpdf->useAdobeCJK = true;

	$mpdf->SetAutoFont(AUTOFONT_ALL);
	$mpdf->list_indent_first_level = 0;	// 1 or 0 - whether to indent the first level of a list
	
	// LOAD a stylesheet
	$stylesheet = file_get_contents($this->ci->config->item('css').'/mpdfstyletables.css');
	$mpdf->WriteHTML($stylesheet,1);	// The parameter 1 tells that this is css/style only and no body/html/text

	$mpdf->WriteHTML($html,2);

	
	$file_name = 'contacts_'.$this->ci->session->userdata['user_id'].'_'.$this->ci->session->userdata['username'].'_'.mktime().'.pdf';
	$pdf = $mpdf->Output('./temp/'.$file_name, 'F'); 
	$data['file'] = $file_name;
	$data['result'] = $pdf;
	
	return $data;
	}
    
    
}
?>
